<#include "../common/base.ftl">
<@body>
<div class="wu-toolbar-search" id="tb">
    <a href="#" id="refresh" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-refresh',plain:true">刷新</a>
    <a href="#" id="add" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-plus',plain:true">手工录单</a>
    <a href="#" id="view" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-pencil-square-o',plain:true">查看明细</a>
    <#--<a href="#" id="del" class="easyui-linkbutton" data-options="iconCls:'icon fa fa-minus-circle',plain:true">删除</a>-->
    <br>
    <label>订单编号：</label><input class="wu-text" id="orderId" style="width:100px">
    <label>客户名称：</label><input class="wu-text" id="buyerName" style="width:100px">
    <label>客户电话：</label><input class="wu-text" id="buyerPhone" style="width:100px">
    <label>订单状态：</label>
    <select id="orderStatus" panelHeight="auto" style="width:120px" class="easyui-combobox"
            data-options="editable:false">
        <option value="-1">订单状态(全部)</option>
        <option value="0">新订单</option>
        <option value="1">完成</option>
        <option value="2">取消</option>
    </select>
    <label>支付状态：</label>
    <select id="payStatus" panelHeight="auto" style="width:120px" class="easyui-combobox"
            data-options="editable:false">
        <option value="-1">支付状态(全部)</option>
        <option value="1">支付成功</option>
        <option value="0">未支付</option>
    </select>
    <label>起始时间：</label><input id="startTime" class="easyui-datebox" style="width:100px" data-options="editable:false">
    <label>结束时间：</label><input id="endTime" class="easyui-datebox" style="width:100px" data-options="editable:false">
    <a class="easyui-linkbutton" iconCls="icon-search" onclick="find()">条件搜索</a>
</div>
<table id="tt" >
</table>
</@body>
<@js>
<script type="text/javascript">
$(function(){
    $('#tt').datagrid({
        singleSelect:false,
        idField:'orderId',
        rownumbers:true,
        toolbar: '#tb',
        pageSize:20,
        pagination:true,
        fitColumns:true,
        fit:true,
        method:'get',
        url:'/order/getData',
        columns:[[
            {field:'orderId',title:'订单编号',width:150,align: 'center'},
            {field:'buyerName',title:'客户名称',width:100,sortable:false,align: 'center'},
            {field:'buyerPhone',title:'客户电话',width:100,sortable:false,align: 'center'},
            {field:'buyerAddress',title:'客户地址',width:100,sortable:false,align: 'center'},
            {field:'buyerOpenid',title:'用户id',width:100,sortable:false,align: 'center'},
            {field:'orderAmount',title:'订单金额',width:100,sortable:false,align: 'center'},
            {field:'orderStatus',title:'订单状态',width:100,sortable:false,align: 'center',
                formatter:function (value,row,index) {
                    var str='';
                    switch (row['orderStatus']){
                        case 0:
                            str='<span style="color: red;">新订单</span>';
                            break;
                        case 1:
                            str='<span style="color: green;">已完成</span>';
                            break;
                        case 2:
                            str='<span style="text-decoration:line-through;">已取消</span>';
                            break;
                        default:str="未定义状态";break;
                    }
                    return str;
                }},
            {field:'payStatus',title:'支付状态',width:100,sortable:false,align: 'center',
                formatter:function (value,row,index) {
                    var str='';
                    switch (row['payStatus']){
                        case 0:
                            str='<span style="color;blue">未支付</span>';
                            break;
                        case 1:
                            str='<span style="color: green;">已支付</span>';
                            break;
                        default:str="未定义状态";break;
                    }
                    return str;
                }},
            {field:'createTime',title:'创建时间',width:80,align:'center'}
        ]]
    });

    $("#startTime").datebox("setValue", myformatter(getCurrentMonthFirst()));
    var curr_time = new Date();
    $("#endTime").datebox("setValue", myformatter(curr_time));

    $("#refresh").click(function () {
        $("#tt").datagrid('reload',{});
    });
    
    $("#view").click(function () {
        var row=$("#tt").datagrid('getSelected');
        if (row==null){
            com.msg_error("请选择要操作的行");
            return false;
        }
        var id=row['orderId'];
        $("<div></div>").dialog({
            id:'viewDialog',
            title:'订单明细',
            resizable: true,
            closable: true,
            width: 800,
            height: 600,
            modal:true,
            href: "/order/view?id="+id,
            onClose:function () {
                $("#viewDialog").dialog('destroy');
            },
            onDestroy: function () {
                $("#tt").datagrid('reload');
            },
            toolbar:[
                {
                    iconCls: 'icon fa fa-times',
                    text: '关闭窗口',
                    handler: function () {
                        $("#viewDialog").dialog('close');
                    }
                },{
                    iconCls: 'icon fa fa-paper-plane',
                    text: '取消订单',
                    handler: function () {
                        $.messager.confirm('确认','您确认想要取消订单吗？',function(r){
                            if (r){
                                cancelOrder(id);
                            }
                        });
                    }
                },{
                    iconCls: 'icon fa fa-check',
                    text: '完成订单',
                    handler: function () {
                        $.messager.confirm('确认','您确认想要完成订单吗？',function(r){
                            if (r){
                                finishOrder(id);
                            }
                        });
                    }
                }
            ]
        });
//        $("#viewDialog").dialog('maximize');
    })
});

function getCurrentMonthFirst() {
    var date = new Date();
    date.setDate(1);
    return date;
}
function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
function find() {
    $('#tt').datagrid('load',{
        startTime:$('#startTime').datebox('getValue'),
        endTime:$('#endTime').datebox('getValue'),
        orderId:$('#orderId').val(),
        buyerName:$('#buyerName').val(),
        buyerPhone:$('#buyerPhone').val(),
        payStatus: $('#payStatus').combobox('getValue'),
        orderStatus: $('#orderStatus').combobox('getValue'),
    })
}
function cancelOrder(id) {
    $.ajax({
        type: 'GET',
        url: "/order/cancel",
        data: {orderId: id},
        success: function (data) {
            var d = eval(data);
            if (data != null && d.msg == 'success') {
                com.msg_success(d.data);
                $("#tt").datagrid('reload');
            } else {
                com.msg_error(d.msg);
            }
        }
    })
}
function finishOrder(id) {
    $.ajax({
        type: 'GET',
        url: "/order/finish",
        data: {orderId: id},
        success: function (data) {
            var d = eval(data);
            if (data != null && d.msg == 'success') {
                $("#viewDialog").dialog('close');
                com.msg_success(d.data);
                $("#tt").datagrid('reload');
            } else {
                com.msg_error(d.msg);
            }
        }
    })
}
</script>
</@js>

